import os
import json
import shutil
htlist=[]
for fname in os.listdir('D:\\Work\\Tool\\tiktok\\TikToks\\'):
    if not fname.endswith('.json'):
        continue
    if not (fname.lower().startswith('hashtag')):
        continue
    ht = fname.split('_')[1].lower()
    if ht in htlist:
        continue
    else:
        htlist.append(ht)
    if not os.path.exists('D:\\tiktok_new\\video_1\\'+ht+'\\'):
        os.makedirs('D:\\tiktok_new\\video_1\\'+ht+'\\')
    with open('D:\\Work\\Tool\\tiktok\\TikToks\\' + fname, 'r', encoding='utf-8', newline='\n') as filename_input:
        i = 0
        for line in filename_input:
            z = json.loads(line)
            if i<=100:
                i += 1
                continue
            else:
                i+=1
            if 'id' in z.keys():
                id = z['id']
                if not os.path.exists('E:\\Tiktok\\Hashtag\\' + ht + '\\' + id + '.mp4'):
                    continue
                vl = z['video']['duration']
                if int(vl)<10:
                    continue
                if not os.path.exists('D:\\tiktok_new\\video_1\\'+ht+'\\' + id + '.mp4'):
                    shutil.copy2('E:\\Tiktok\\Hashtag\\' + ht + '\\' + id + '.mp4','D:\\tiktok_new\\video_1\\'+ht+'\\' + id + '.mp4')
            elif 'itemInfos' in z.keys():
                id = z['itemInfos']['id']
                if not os.path.exists('E:\\Tiktok\\Hashtag\\' + ht + '\\' + id + '.mp4'):
                    continue
                vl = z['itemInfos']['video']['videoMeta']['duration']
                if int(vl)<10:
                    continue
                if not os.path.exists('D:\\tiktok_new\\video_1\\'+ht+'\\' + id + '.mp4'):
                    shutil.copy2('E:\\Tiktok\\Hashtag\\' + ht + '\\' + id + '.mp4','D:\\tiktok_new\\video_1\\'+ht+'\\' + id + '.mp4')

            if i>300:
                break